//
//  TimeLineView.m
//  Exam13-2b
//
//  Created by 孙晓晔 on 2017/4/29.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "TimeLineView.h"
#import "Tally.h"
#import "TallyListCellModel.h"

@interface TimeLineView()
@property(nonatomic,strong) NSArray *tallyListArray;
@property(nonatomic,strong) UIView *panelView;
@end

@implementation TimeLineView

-(NSArray *)tallyListArray {
    if (_tallyListArray == nil) {
        NSString *strPath = [[NSBundle mainBundle]pathForResource:@"TallyList.plist" ofType:nil];
        NSArray *arr = [NSArray arrayWithContentsOfFile:strPath];
        
        NSMutableArray *muArr = [NSMutableArray array];
        
        for (NSDictionary *dict in arr) {
            TallyListCellModel *obj = [TallyListCellModel tallyListCellModelWithDict:dict];
            
            [muArr addObject:obj];
        }
        
        _tallyListArray = muArr;
    }
    
    return _tallyListArray;
}

// 画按钮
- (void)drawBtn:(CGRect)btnRect model:(TallyListCellModel *)model index:(NSInteger)index {
    UIButton *btn = [[UIButton alloc]initWithFrame:btnRect];
    
    btn.tag = index;
    btn.layer.masksToBounds = YES;
    
    [btn setImage:[UIImage imageNamed:model.tallyCellImage] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(panel:) forControlEvents:UIControlEventTouchUpInside];
        
    [self addSubview:btn];
}

// 画竖线
- (void)drawLine:(CGFloat)start end:(CGFloat)end {
  
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(context, self.center.x, start);
    CGContextAddLineToPoint(context, self.center.x, end);
    CGContextSetLineWidth(context, kLineWidth);
    
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    
    CGContextStrokePath(context);
}

// 画收支
- (void)drawLabel:(Tally *)tally btnRect:(CGRect)btnRect model:(TallyListCellModel *)model {
    
    CGFloat labelX = self.center.x + kBtnWidth;
        
    if ([tally.type isEqualToString:@"1"]) {
        labelX = 0;
    }
        
    CGFloat labelY = btnRect.origin.y;
    CGFloat labelWidth = self.frame.size.width/2 - kBtnWidth;
    CGFloat labelHeight = btnRect.size.height;
        
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, labelHeight)];
        
    if ([tally.type isEqualToString:@"1"]) {
        label.textAlignment = NSTextAlignmentRight;
    } else {
        label.textAlignment = NSTextAlignmentLeft;
    }
        
    label.text = [NSString stringWithFormat:@"%@ %.2f", model.tallyCellName, tally.money];
    [self addSubview:label];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGFloat aDateAllLine = 0;
    
    for (int i = 0; i < self.tallyArr.count; i++) {
        
        // 画竖线
        CGFloat start = aDateAllLine + (kLineHeight+kBtnWidth)*i;
        CGFloat end = start + kLineHeight;
        
        [self drawLine:start end:end];
        
        // 画按钮
        CGRect btnRect = CGRectMake(self.center.x - kBtnWidth/2, end, kBtnWidth, kBtnWidth);
        
        Tally *tally = (Tally*)_tallyArr[i];
        TallyListCellModel *model = self.tallyListArray[tally.icon];
        
        [self drawBtn:btnRect model:model index:i];
        
        // 画收支
        [self drawLabel:tally btnRect:btnRect model:model];
    }
}

// 左删除
- (void)leftBtn:(UIButton*)btn {
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width/4 - kBtnWidth/2, 0, kBtnWidth, kBtnWidth)];
    
    leftBtn.backgroundColor = [UIColor clearColor];
    leftBtn.tag = btn.tag;
    
    [leftBtn setImage:[UIImage imageNamed:@"删除#时光轴_25x25_"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(deleteCurrentTally:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.panelView addSubview:leftBtn];
    [self btnShowAnimation:leftBtn right:false];
}

// 右修改
- (void)rightBtn:(UIButton*)btn {
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width*3/4 - kBtnWidth/2, 0, kBtnWidth, kBtnWidth)];
    
    rightBtn.backgroundColor = [UIColor clearColor];
    rightBtn.tag = btn.tag;
    
    [rightBtn setImage:[UIImage imageNamed:@"修改#时光轴_25x25_"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(modifyCurrentTally:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.panelView addSubview:rightBtn];
    [self btnShowAnimation:rightBtn right:true];
}

// 中间按钮
- (void)middleBtn:(UIButton*)btn {
    UIButton *middleBtn = [[UIButton alloc] initWithFrame:CGRectMake(btn.frame.origin.x, 0, btn.frame.size.width, btn.frame.size.height)];
    
    middleBtn.backgroundColor = [UIColor clearColor];
    
    [middleBtn setImage:btn.imageView.image forState:UIControlStateNormal];
    [middleBtn addTarget:self action:@selector(clickMiddleBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.panelView addSubview:middleBtn];
}

// 管理面板
-(void)panel:(UIButton*)btn {
    NSLog(@"%ld", btn.tag);
    
    if (self.panelView) {
        [self.panelView removeFromSuperview];
    }
    
    self.panelView = [[UIView alloc] initWithFrame:CGRectMake(0, btn.frame.origin.y, self.frame.size.width, btn.frame.size.height)];
    self.panelView.backgroundColor = [UIColor whiteColor];
    self.panelView.userInteractionEnabled = YES;
    
    [self addSubview:self.panelView];
    
    [self leftBtn:btn];
    
    [self rightBtn:btn];
    
    [self middleBtn:btn];
}

//btn出现时动画
- (void)btnShowAnimation:(UIButton*)btn right:(BOOL)right{
    btn.backgroundColor = [UIColor whiteColor];
    CABasicAnimation *an = [CABasicAnimation animation];
    an.keyPath = @"position";
    
    // CGFloat x = btn.panelBtnType ? self.frame.size.width * 3/4:self.frame.size.width/4;
    CGFloat x = right ? self.frame.size.width * 3/4:self.frame.size.width/4;
    
    an.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, btn.center.y)];
    an.toValue = [NSValue valueWithCGPoint:CGPointMake(x, btn.center.y)];
    an.duration = 0.5;
    an.removedOnCompletion = NO;
    an.fillMode = kCAFillModeForwards;
    
    [btn.layer addAnimation:an forKey:nil];
}

//点击中间按钮 控制板收回
- (void)clickMiddleBtn{
    for (UIButton *btn in self.panelView.subviews) {
        [self btnDismissAnimation:btn];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self.panelView removeFromSuperview];
    }];
}

//btn消失时动画
- (void)btnDismissAnimation:(UIButton*)btn{
    btn.backgroundColor = [UIColor clearColor];
    
    CABasicAnimation *an = [CABasicAnimation animation];
    an.keyPath = @"position";
    an.toValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, btn.center.y)];
    an.duration = 0.5;
    an.removedOnCompletion = NO;
    an.fillMode = kCAFillModeForwards;
    
    [btn.layer addAnimation:an forKey:nil];
}

//删除当前账单  回调给controller处理
- (void)deleteCurrentTally:(UIButton*)btn {
    if ([self.delegate respondsToSelector:@selector(deleteTally:)]) {
        Tally *tally = (Tally*)_tallyArr[btn.tag];
        
        [self.delegate deleteTally:tally];
    }
}

//修改当前账单  回调给controller处理
- (void)modifyCurrentTally:(UIButton*)btn {
    if ([self.delegate respondsToSelector:@selector(editTally:)]) {
        Tally *tally = (Tally*)_tallyArr[btn.tag];
        
        [self.delegate editTally:tally];
    }
}

@end
